import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../../config/public_color.dart';
import '../divider/public_divider.dart';
import 'status_bar.dart';

/// FileName public_appbar
///
/// @Author lax
/// @Date 2023/3/14
///
/// @Description

typedef AppBarBuilder = Widget Function(AppBar appBar);

class PublicAppBar extends StatelessWidget implements PreferredSizeWidget {
  static AppBarBuilder? defaultAppBarBuilder;

  static Function(BuildContext context)? defaultOnBack;

  static Widget? defaultLeading;

  static TextStyle? defaultTitleStyle;

  static double? defaultToolHeight = 44;

  static Decoration? defaultDecoration;

  static Widget? defaultBackWidget;
  static Widget? defaultFrontWidget = const PublicDivider();

  static Brightness? defaultStatusBarBrightness;

  late final AppBar appBar;

  /// 自定义AppBar样式
  late final AppBarBuilder? appBarBuilder;

  late final Function(BuildContext context)? onBack;

  late final Widget? leading;
  final double? leadingWidth;

  /// 自定义Title，title和titleStyle会失效
  final Widget? titleWidget;
  final String? title;
  late final TextStyle titleStyle;

  final List<Widget>? actions;

  final double? rightMargin;
  late final double? toolHeight;

  /// 背景
  late final Color color;
  late final Decoration? decoration;

  late final Widget? backWidget;
  late final Widget? frontWidget;

  /// 状态栏样式 默认根据color适配
  late final Brightness? statusBarBrightness;

  final bool isClearColor;

  PublicAppBar({
    super.key,
    AppBarBuilder? appBarBuilder,
    Function(BuildContext context)? onBack,
    Widget? leading,
    bool isShowLeading = true,
    this.leadingWidth,
    this.title,
    TextStyle? titleStyle,
    this.titleWidget,
    this.actions,
    this.rightMargin = 4,
    double? toolHeight,
    Color? color,
    Decoration? decoration,
    Widget? backWidget,
    Widget? frontWidget,
    Brightness? statusBarBrightness,
    this.isClearColor = false,
  }) {
    this.appBarBuilder = appBarBuilder ?? defaultAppBarBuilder;
    this.onBack = onBack ?? defaultOnBack;
    this.titleStyle = titleStyle ??
        defaultTitleStyle ??
        TextStyle(
          fontSize: 20,
          fontWeight: FontWeight.bold,
          color: PublicColor.darkText,
        );
    this.toolHeight = toolHeight ?? defaultToolHeight;
    this.color = color ?? PublicColor.appBar;
    this.decoration = decoration ?? defaultDecoration;
    this.backWidget = backWidget ?? defaultBackWidget;
    this.frontWidget = frontWidget ?? defaultFrontWidget;
    this.statusBarBrightness =
        statusBarBrightness ?? defaultStatusBarBrightness;

    this.leading = leading ??
        defaultLeading ??
        Builder(
          builder: (context) {
            return TextButton(
              child: Icon(
                Icons.arrow_back_ios_new,
                color: this.titleStyle.color,
                size: 20,
              ),
              onPressed: () {
                if (this.onBack != null) {
                  this.onBack?.call(context);
                } else {
                  Navigator.maybePop(context);
                }
              },
            );
          },
        );

    if ((actions ?? []).isNotEmpty && rightMargin != null) {
      (actions ?? []).add(SizedBox(width: rightMargin));
    }

    appBar = AppBar(
      leading: isShowLeading ? this.leading : null,
      automaticallyImplyLeading: false,
      // 默认不显示返回按钮
      leadingWidth: leadingWidth ?? 44,
      toolbarHeight: this.toolHeight,
      title: titleWidget ?? Text(title ?? '', style: this.titleStyle),
      centerTitle: true,
      actions: actions,
      backgroundColor: Colors.transparent,
      systemOverlayStyle: SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,
        statusBarIconBrightness: this.statusBarBrightness ??
            (ThemeData.estimateBrightnessForColor(this.color) == Brightness.dark
                ? Brightness.light
                : Brightness.dark),
      ),
      elevation: 0,
      scrolledUnderElevation: 0,
    );
  }

  @override
  Widget build(BuildContext context) {
    late Widget bar;
    if (kIsWeb) {
      bar = Column(
        children: [
          StatusBar(),
          appBarBuilder?.call(appBar) ?? appBar,
        ],
      );
    } else {
      bar = appBarBuilder?.call(appBar) ?? appBar;
    }
    return Container(
      decoration:
          isClearColor ? null : (decoration ?? BoxDecoration(color: color)),
      height: preferredSize.height,
      child: Stack(
        alignment: Alignment.bottomCenter,
        children: [
          Visibility(
            visible: !isClearColor,
            child: backWidget ?? const SizedBox(),
          ),
          bar,
          Visibility(
            visible: !isClearColor,
            child: frontWidget ?? const SizedBox(),
          ),
        ],
      ),
    );
  }

  @override
  Size get preferredSize {
    return Size(appBar.preferredSize.width, height());
  }

  static double height([BuildContext? context]) {
    return (defaultToolHeight ?? 0) + StatusBar.height(context);
  }
}
